/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  NoiseIdentifyRuleHelper.java                                      
 *  File Contents Outline :  噪声规则鉴定持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbNoiseIdentifyRule;
import storage.util.StorageManager;


public class NoiseIdentifyRuleHelper {
     /**
     * 将清洗规则保存到LaUDMS数据库中
     *
     * @param noiseRule 要存储的模型对象
     */
     public void saveNoiseRule(DbNoiseIdentifyRule noiseRule) {
        //StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_noiseRule);
                                              PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_noiseRule"));
//        sm.open();
        sm.setRowKey(noiseRule.getRowKey());
        sm.put(DbNoiseIdentifyRule.cl_name, noiseRule.getName());
        sm.put(DbNoiseIdentifyRule.cl_noiseRule, noiseRule.getNoiseRule());
        sm.put(DbNoiseIdentifyRule.cl_createTime, noiseRule.getCreateTime());      
//        sm.close();
    }
     /**
      * 
      * @param columnName
      * @param value
      * @return 
      */
       public List<DbNoiseIdentifyRule> getNoiseRules(final String columnName, final String value) {
        List<DbNoiseIdentifyRule> result = new LinkedList<DbNoiseIdentifyRule>();
                                              PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_noiseRule"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String name = null, noiseRule = null, createTime = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbNoiseIdentifyRule.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbNoiseIdentifyRule.cl_noiseRule)) {
                        noiseRule = columnsValue;
                    } else if (columnsName.equals(DbNoiseIdentifyRule.cl_createTime)) {
                        createTime = columnsValue;
                    }
                }
                DbNoiseIdentifyRule pLogNoiseRule = new DbNoiseIdentifyRule(rowKey, name, noiseRule, createTime);
                result.add(pLogNoiseRule);
            }
        } catch (Exception ex) {
            Logger.getLogger(NoiseIdentifyRuleHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public ArrayList<String> getAllNoiseRuleName() {
                                              PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_noiseRule"));
//        sm.open();
        List<KeySlice> crs = sm.getAll();
        ArrayList<String> noiseRuleNameList = new ArrayList<String>();
        try {
            for (KeySlice ks : crs) {
                String name = "";
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbNoiseIdentifyRule.cl_name)) {
                        name = columnsValue;
                    }
                }
                if (!noiseRuleNameList.contains(name)&&name.length()>0) {
                    noiseRuleNameList.add(name);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(NoiseIdentifyRuleHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return noiseRuleNameList;
    }
    
    public void deleteNoiseRule(final String rowKey){
                                              PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_noiseRule"));
        sm.delete(rowKey);
//        sm.close();
    }
}
